package com.wwc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwc.business.controller.dto.train_carriage.TrainCarriageDeleteDTO;
import com.wwc.business.controller.dto.train_carriage.TrainCarriageInsertDTO;
import com.wwc.business.controller.dto.train_carriage.TrainCarriageSelectListDTO;
import com.wwc.business.controller.dto.train_carriage.TrainCarriageUpdateDTO;
import com.wwc.business.controller.vo.TrainCarriageSelectListVO;
import com.wwc.business.entity.TrainCarriage;
import com.wwc.business.enums.SeatColEnum;
import com.wwc.business.mapper.TrainCarriageMapper;
import com.wwc.business.service.ITrainCarriageService;
import com.wwc.common.exception.BusinessException;
import com.wwc.common.exception.BusinessExceptionEnum;
import com.wwc.common.response.PageResponse;
import com.wwc.common.utils.SnowUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TrainCarriageServiceImpl extends ServiceImpl<TrainCarriageMapper, TrainCarriage> implements ITrainCarriageService{
	@Resource
	private TrainCarriageMapper trainCarriageMapper;

	@Override
	public PageResponse<TrainCarriageSelectListVO> selectListPage(TrainCarriageSelectListDTO trainCarriageSelectListDTO){
		// 根据查询条件和分页配置查询火车车厢分页列表
		Page<TrainCarriage> page = new Page<>(trainCarriageSelectListDTO.getPage(), trainCarriageSelectListDTO.getSize());
		QueryWrapper<TrainCarriage> trainCarriageQueryWrapper = new QueryWrapper<>();
		trainCarriageQueryWrapper.orderByAsc("train_code").orderByAsc("`index`");
		if(StrUtil.isNotBlank(trainCarriageSelectListDTO.getTrainCode())){
			trainCarriageQueryWrapper.eq("train_code", trainCarriageSelectListDTO.getTrainCode());
		}
		Page<TrainCarriage> trainCarriagePage = trainCarriageMapper.selectPage(page, trainCarriageQueryWrapper);

		// 返回自定义分页配置类
		PageResponse<TrainCarriageSelectListVO> pageResponse = new PageResponse<>();
		pageResponse.setTotal(trainCarriagePage.getTotal());
		pageResponse.setList(BeanUtil.copyToList(trainCarriagePage.getRecords(), TrainCarriageSelectListVO.class));
		return pageResponse;
	}

	@Override
	public void delete(TrainCarriageDeleteDTO trainCarriageDeleteDTO){
		// 判断想要删除的数据是否存在
		TrainCarriage tc = trainCarriageMapper.selectById(trainCarriageDeleteDTO.getId());
		if(ObjectUtil.isNull(tc)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DELETE_DATA_DOES_NOT_EXIST);
		}

		// 删除火车车厢
		trainCarriageMapper.deleteById(trainCarriageDeleteDTO.getId());
	}

	@Override
	public void update(TrainCarriageUpdateDTO trainCarriageUpdateDTO){
		// 判断想要修改的数据是否存在
		TrainCarriage tc = trainCarriageMapper.selectById(trainCarriageUpdateDTO.getId());
		if(ObjectUtil.isNull(tc)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DATA_DOES_NOT_EXIST);
		}

		// 根据查询条件查询火车列表
		QueryWrapper<TrainCarriage> trainCarriageQueryWrapper = new QueryWrapper<>();
		trainCarriageQueryWrapper.eq("`train_code`", trainCarriageUpdateDTO.getTrainCode());
		trainCarriageQueryWrapper.eq("`index`", trainCarriageUpdateDTO.getIndex());
		trainCarriageQueryWrapper.ne("id", trainCarriageUpdateDTO.getId());
		List<TrainCarriage> trainList = trainCarriageMapper.selectList(trainCarriageQueryWrapper);
		// 根据车次编号查询如果为空 或者 车次编号和要修改的车次是同一条数据，反之抛出异常
		if(CollUtil.isNotEmpty(trainList)){
			throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR);
		}

		// 自动生成列数，座位总数
		List<SeatColEnum> seatColEnumList = SeatColEnum.getColsByType(trainCarriageUpdateDTO.getSeatType());
		trainCarriageUpdateDTO.setColCount(seatColEnumList.size());
		trainCarriageUpdateDTO.setSeatCount(trainCarriageUpdateDTO.getColCount() * trainCarriageUpdateDTO.getRowCount());

		// 修改火车车厢
		TrainCarriage trainCarriage = BeanUtil.copyProperties(trainCarriageUpdateDTO, TrainCarriage.class);
		trainCarriage.setUpdateTime(new Date());
		trainCarriageMapper.updateById(trainCarriage);
	}

	@Override
	public void insert(TrainCarriageInsertDTO trainCarriageInsertDTO){
		// 根据查询条件获取火车列表
		QueryWrapper<TrainCarriage> trainCarriageQueryWrapper = new QueryWrapper<>();
		trainCarriageQueryWrapper.eq("`train_code`", trainCarriageInsertDTO.getTrainCode());
		trainCarriageQueryWrapper.eq("`index`", trainCarriageInsertDTO.getIndex());
		List<TrainCarriage> trainCarriageList = trainCarriageMapper.selectList(trainCarriageQueryWrapper);
		// 如果火车编号已存在则抛出异常
		if(CollUtil.isNotEmpty(trainCarriageList)){
			throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR);
		}

		// 自动生成列数，座位总数
		List<SeatColEnum> seatColEnumList = SeatColEnum.getColsByType(trainCarriageInsertDTO.getSeatType());
		trainCarriageInsertDTO.setColCount(seatColEnumList.size());
		trainCarriageInsertDTO.setSeatCount(trainCarriageInsertDTO.getColCount() * trainCarriageInsertDTO.getRowCount());

		// 新增火车车厢
		TrainCarriage trainCarriage = BeanUtil.copyProperties(trainCarriageInsertDTO, TrainCarriage.class);
		trainCarriage.setId(SnowUtil.getSnowflakeNextId());
		trainCarriage.setCreateTime(new Date());
		trainCarriage.setUpdateTime(new Date());
		trainCarriageMapper.insert(trainCarriage);
	}
}
